<?php
include "dbconnect.php";
$nameErr=$pwd1err=$pwd2err=$fnameerr=$lnameerr=$yearerr=$gpaerr=$degreeerr=$programerr=$tepaicerr=$iuemailerr=$alreadyexistErr="";
$name=$pwd1=$pwd2=$tepaic=$fname=$lname=$experience=$year=$degree=$program=$mismatch=$emailid="";
$gpa=0.0;
if ($_SERVER["REQUEST_METHOD"] == "POST")
{

$prog=$_POST["regprgrm"];
$deg=$_POST["regdegree"];
$tp=$_POST["regtepaic"];
$yr=$_POST["regyear"];
$experience=$_POST["regexp"];
  if (empty($_POST["regUsername"]))
    {$nameErr = "Username is required";}
  else
    {
    $name = clean_input($_POST["regUsername"]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$name))
      {
      $nameErr = "Invalid Username";
      }
    }
	
	if (empty($_POST["regPassword1"]))
    {$pwd1err = "Password is required";}
  else
    {
    $pwd1 = clean_input($_POST["regPassword1"]);
    if (!preg_match("/^[a-zA-Z ]*$/",$pwd1))
      {
      $pwd1err = "Invalid password";
      }
    }
	
	if (empty($_POST["regPassword2"]))
    {$pwd2err = "Retype Password is required";}
  else
    {
    $pwd2 = clean_input($_POST["regPassword2"]);
    if (!preg_match("/^[a-zA-Z ]*$/",$pwd2))
      {
      $pwd2error = "Invalid password";
      }
	}
	if ($pwd1!=$pwd2)
	{
		$mismatch="password does not match";
	}
	
	if (empty($_POST["regfname"]))
    {$fnameerr = "Firstname is required";}
  else
    {
    $fname = clean_input($_POST["regfname"]);
    if (!preg_match("/^[a-zA-Z ]*$/",$fname))
      {
      $fnameerr = "Invalid Firstname";
      }
	}
	
	if (empty($_POST["reglname"]))
    {$lnameerr = "Lastname is required";}
  else
    {
    $lname = clean_input($_POST["reglname"]);
    if (!preg_match("/^[a-zA-Z ]*$/",$lname))
      {
      $lnameerr = "Invalid Lastname";
      }
	}
	if (empty($_POST["regiumail"]))
    {}
  else
    {
    $emailid = clean_input($_POST["regiumail"]);
    if (!preg_match("#^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,6}$#" , $emailid))
      {
      $iuemailerr = "Invalid Email format";
      }
	}

	if (empty($_POST["regGPA"]))
    {$gpaerr = "GPA is required";}
  else
    {
    $gpa = clean_input($_POST["regGPA"]);
    if (!preg_match("/[0-4][.][0-9]$/",$gpa))
      {
      $gpaerr = "Invalid GPA format";;
      }
	}
}

function clean_input($data)
{
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}

if($nameErr=="" and $pwd1err=="" and $pwd2err=="" and $fnameerr=="" and $lnameerr=="" and $gpaerr=="" and $iuemailerr=="")
{
if(!empty($name))
	{
		//checking is username already exixte
		
$con=mysql_connect($dbHost,$dbUserAndName,$dbPass);
if(!$con)
{
	die("Error: ".mysql_error());
	}
mysql_select_db($_DB_NAME,$con);
			
				$check="SELECT username FROM students WHERE username = '".$_POST['regUsername']."'";
				if (mysql_num_rows(mysql_query($check)) != 0)
				 {
          // code to run if result is not 0 (i.e. already exists)
		  			$alreadyexistErr= "User already exists";
			     }


				else
						{	
							$sql="INSERT INTO users (username,password,FirstName,LastName,Typeid) VALUES('$name','$pwd1','$fname','$lname',3)";
							if (!mysql_query($sql,$con))
							  {
								die('Error:user table ');
							}
							mysql_query("SET @pdeg='".$_POST["regdegree"]."'");
							$result=mysql_query("select degreeid from degree where description=@pdeg");
							if( $row = mysql_fetch_array($result, MYSQL_ASSOC) )
							{
								$id = $row['degreeid'];
							}
							else
							{
								echo "no recordes";// No records matched query.
							} 
			  
							$sql1="INSERT INTO students (username,Emailid,Degree,Program,Year, GPA,TEPAIC,Experience)
			VALUES('$name','$emailid',$id,'$prog',$yr,$gpa,'$tp','$experience')";
			
						if(!mysql_query($sql1,$con))
						  {
											die('Error:student table ');
						  }
			  			header('location:successreg.php');
					}
	}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>AIMS</title>

<link href="CSS/Global.css" type="text/css" rel="stylesheet" />
<link href="CSS/Base.css" type="text/css" rel="stylesheet" />
<link href="CSS/Fixed.css" type="text/css" rel="stylesheet" />
<link rel="stylesheet" href="jqwidgets/styles/jqx.base.css" type="text/css" />

</head>

<body style="min-width:1280px;background:none">

<div id="branding-bar" class="gateway">
<div class="bar">
<div class="wrapper">
<h1 class="campus"><a><img  height="73" src="Resources/img/trident-tab.gif.png" width="64"/>AIMS</a></h1>
</div>
</div>
</div>
<div class="registrationbody">
<form method="post" action="<?php echo basename(htmlspecialchars($_SERVER["PHP_SELF"]));?>" style="vertical-align:central">
<table class="registrationtable">
<tbody>    
    <tr>
    <label class="Header1">Registration Form</label>
    </tr>  
    <tr />
    <tr />
    <tr />
    <tr />
    <tr />
    <tr />
    <tr />
    <tr />
    <tr />
    <tr />
    <tr />
    <tr />
    <tr />
    <tr />
    <tr />
    <tr />
    <tr />
    <tr />
    <tr>
    	<td>
        <label class="crimson-text">Username</label>
        </td>
        <td>
        <input type="text" name="regUsername" value="<?php echo $name;?>" class="crimson-border"/>
        </td>
        <td class="error">
        <span class="error" style="width:90%"> <?php echo $nameErr.$alreadyexistErr;?></span>
        </td>
        <td>
        <label  class="crimson-text">Password</label>
        </td>
        <td>
        <input type="password" name="regPassword1" value="<?php echo $pwd1;?>" class="crimson-border"/>
        </td>
        <td class="error">
        <span class="error" style="width:90%"> <?php echo $pwd1err;?></span>
        </td>
    </tr>
    <tr />
    <tr />
    <tr />
    <tr />
    <tr />
    <tr />
    <tr />
    <tr />
    <tr />
    <tr />
    <tr />
    <tr>
    	<td>
        <label class="crimson-text">GPA</label>
        </td>
        <td>
        <input type="text" name="regGPA" value="<?php echo $gpa;?>" class="crimson-border" />
        </td>
        <td class="error">
        <span class="error" style="width:90%"><?php echo $gpaerr;?></span>
        </td>
        <td>
        <label  class="crimson-text">Retype Password</label>
        </td>
        <td>
        <input type="password" name="regPassword2" value="<?php echo $pwd2;?>" class="crimson-border"/>
        </td>
        <td class="error">
        <span class="error" style="width:90%"><?php echo $pwd2err;?><?php echo $mismatch;?></span>
        </td>
    </tr>
     <tr />
    <tr />
    <tr />
    <tr />
    <tr />
    <tr />
    <tr />
    <tr />
    <tr />
    <tr />
    <tr />
    <tr>
    	<td>
        <label  class="crimson-text">Firstname</label>
        </td>
        <td>
        <input type="text" name="regfname" value="<?php echo $fname;?>" class="crimson-border"/>
        </td>
        <td class="error">
        <span class="error" style="width:90%"> <?php echo $fnameerr;?></span>
        </td>
        <td>
        <label  class="crimson-text">Lastname</label>
        </td>
        <td>
        <input type="text" name="reglname" value="<?php echo $lname;?>" class="crimson-border"/>
        </td>
        <td class="error">
        <span class="error" style="width:90%"> <?php echo $lnameerr;?></span>
        </td>
    </tr>
     <tr />
    <tr />
    <tr />
    <tr />
    <tr />
    <tr />
    <tr />
    <tr />
    <tr />
    <tr />
    <tr />
    <tr>
    	<td>
        <label  class="crimson-text">TEPAIC Score</label>
        </td>
        <td>
        <!--<input type="text" name="regtepaic" value="<?php echo $tepaic;?>"/>-->
         <select name="regtepaic" class="crimson-text" >
                    
                     <option value=""<?php
					  if (isset($tp) && $tp=="") echo "selected='selected'";
					  else echo "";?>></option>
                   <option value="C1"<?php
					  if (isset($tp) && $tp=="C1") echo "selected='selected'";
					  else echo "";?>>C1</option>
                   <option value="C2"<?php
					  if (isset($tp) && $tp=="C2") echo "selected='selected'";
					  else echo "";?>>C2</option>
                   <option value="C3"<?php
					  if (isset($tp) && $tp=="C3") echo "selected='selected'";
					  else echo "";?>>C3</option>
                      <option value="NC4"<?php
					  if (isset($tp) && $tp=="NC4") echo "selected='selected'";
					  else echo "";?>>NC4</option>
                   
                   <option value="NC4"<?php
					  if (isset($tp) && $tp=="NC4") echo "selected='selected'";
					  else echo "";?>>NC5</option>

                      </select>
        </td>
        <td class="error">
        <!--<span class="error" style="width:90%">* <?php echo $tepaicerr;?></span>-->
        </td>
       
        
        <td>
        <label class="crimson-text">IUemailid</label>
        </td>
        <td>
        <input type="text" name="regiumail" class="crimson-border" value="<?php echo $emailid;?>"/>
        </td>
        <td class="error">
        <span class="error" style="width:90%"> <?php echo $iuemailerr;?></span>
        </td>
    </tr>
     <tr />
    <tr />
    <tr />
    <tr />
    <tr />
    <tr />
    <tr />
    <tr />
    <tr />
    <tr />
    <tr />
    <tr>
    	<td>
        <label class="crimson-text">Degree</label>
        </td>
        <td>
       <!-- <input type="text" name="regdegree" value="<?php echo $degree;?>"/>-->
       <select name="regdegree" class="crimson-text" >
                    
                     <option value="BS"<?php
					  if (isset($deg) && $deg=="BS") echo "selected='selected'";
					  else echo "";?>>BS</option>
                   <option value="MS"<?php
					  if (isset($deg) && $deg=="MS") echo "selected='selected'";
					  else echo "";?>>MS</option>
                   <option value="PhD"<?php
					  if (isset($deg) && $deg=="PhD") echo "selected='selected'";
					  else echo "";?>>PhD</option>
                   

        </select>
        </td>
        <td class="error">
      <!--  <span class="error" style="width:90%">* <?php echo $degreeerr;?></span>-->
        </td>
        
        
        <td>
        <label class="crimson-text">Year</label>
        </td>
        <td>
        <!--<input type="text" name="regyear" value="<?php echo $year;?>" />-->
        
        <select name="regyear" class="crimson-text" >
        <option value="1"<?php
					  if (isset($yr) && $yr=="1") echo "selected='selected'";
					  else echo "";?>>1</option>
                      <option value="2"<?php
					  if (isset($yr) && $yr=="2") echo "selected='selected'";
					  else echo "";?>>2</option>
                      <option value="3"<?php
					  if (isset($yr) && $yr=="3") echo "selected='selected'";
					  else echo "";?>>3</option>
                      <option value="4"<?php
					  if (isset($yr) && $yr=="4") echo "selected='selected'";
					  else echo "";?>>4</option>
                      <option value="5"<?php
					  if (isset($yr) && $yr=="5") echo "selected='selected'";
					  else echo "";?>>5</option>
                      <option value="6"<?php
					  if (isset($yr) && $yr=="6") echo "selected='selected'";
					  else echo "";?>>6</option>
                      <option value="7"<?php
					  if (isset($yr) && $yr=="7") echo "selected='selected'";
					  else echo "";?>>7</option>
                      <option value="8"<?php
					  if (isset($yr) && $yr=="8") echo "selected='selected'";
					  else echo "";?>>8</option>
                      <option value="9"<?php
					  if (isset($yr) && $yr=="9") echo "selected='selected'";
					  else echo "";?>>9</option>
                      
        </select>
        </td>
        <td class="error">
       <!-- <span class="error" style="width:90%"> <?php echo $yearerr;?></span>-->
        </td>
    </tr>
     <tr />
    <tr />
    <tr />
    <tr />
    <tr />
    <tr />
    <tr />
    <tr /><tr />
    <tr />
    <tr />
    <tr>
    	<td>
        <label class="crimson-text">Program</label>
        </td>
        <td>
        <!--<input type="text" name="regprgrm" value="<?php echo $program;?>"/>-->
        <select name="regprgrm" class="crimson-text" >
                    
                     <option value="Computer Science"<?php
					  if (isset($prog) && $prog=="Computer Science") echo "selected='selected'";
					  else echo "";?>>Computer Science</option>
                  
          </select>
        </td>
        <td class="error">
        <!--<span class="error" style="width:90%">* <?php echo $programerr;?></span>-->
        </td>
         <td>
        <label class="crimson-text">Experience</label>
        </td>
        <td>
        <input type="text" name="regexp" value="<?php echo $experience;?>" class="crimson-border"/>
        </td>
        <td/>
        
        
      </tr>
       <tr />
    <tr />
    <tr />
    <tr />
    <tr />
    <tr />
    <tr />
    <tr />
    <tr />
    <tr />
    <tr />
        <tr>
      
        <td>
        <input type="submit" value="Register"></input>
        </td>
    </tr>
</tbody>
</table>
</form>
</div>
</body>
</html>